package com.example.cinema.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.cinema.entity.Hall;
import com.example.cinema.entity.Movie;
import com.example.cinema.entity.Schedule;
import com.example.cinema.mapper.HallMapper;
import com.example.cinema.mapper.MovieMapper;
import com.example.cinema.mapper.ScheduleMapper;
import com.example.cinema.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yc138
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    MovieMapper movieMapper;
    @Autowired
    HallMapper hallMapper;
    @Autowired
    ScheduleMapper scheduleMapper;
    @RequestMapping("queryMovies")
    public List<Movie> queryMovies(String search){
        LambdaQueryWrapper<Movie> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Movie::getStatus,1);
        lqw.or();
        lqw.eq(Movie::getStatus,2);
        if (search != null && search.isBlank() == false){
            lqw.and(wrapper -> wrapper.like(Movie::getName, search).or().like(Movie::getType, search));
        }
        return  movieMapper.selectList(lqw);
    }

    @RequestMapping("queryHalls")
    public List<Hall> queryHalls(){
        return hallMapper.findAllByHall();
    }

    @RequestMapping("addSchedule")
    @Transactional
    public Result addSchedule(@RequestBody @Valid Schedule schedule, Errors errors){
        //添加到排片表，并更新电影状态

        if (errors.hasErrors()) {
            return new Result(0,"请完善排片信息",errors.getAllErrors());
        }

        // 检查排片时间是否冲突
        LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Schedule::getHid, schedule.getHid());
        // 检查新排片的开始时间是否在已有排片的结束时间之后或同时开始
        lqw.lt(Schedule::getStart, schedule.getEnd());
        // 检查新排片的结束时间是否在已有排片的开始时间之前或同时结束
        lqw.gt(Schedule::getEnd, schedule.getStart());
        List<Schedule> conflictingSchedules = scheduleMapper.selectList(lqw);
        if (!conflictingSchedules.isEmpty()) {
            return new Result(0, "排片时间冲突", null);
        }
        //更新电影状态
        LambdaQueryWrapper<Movie> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Movie::getId,schedule.getMid());
        Movie movie = new Movie();
        movie.setStatus(2);
        movieMapper.update(movie,lqw2);
        scheduleMapper.insert(schedule);

        return new Result(1,"排片成功！",null);
    }

    @RequestMapping("querySchedules")
    public List<Schedule> querySchedules(String movieame,String hid,
                                        @RequestParam(defaultValue = "1") int current,
                                         @RequestParam(defaultValue = "5") int size){
        LambdaQueryWrapper<Movie> lqw = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Schedule> lqw2 = new LambdaQueryWrapper<>();
        List<Schedule> allSchedule = new ArrayList<>();
        int total = 0;
        Movie movie = null;
        if (movieame != null && movieame.isBlank() == false){
            lqw.like(Movie::getName,movieame);
            movie = movieMapper.selectOne(lqw);
            if (movie != null){
                allSchedule = scheduleMapper.findByMidSchedule(movie.getId(),(current - 1) * size, size);
                lqw2.eq(Schedule::getMid,movie.getId());
                total = scheduleMapper.selectList(lqw2).size();
            }

        }

        if (hid != null && hid.isBlank() == false && !hid.equals("0")){
            allSchedule = scheduleMapper.findByHidSchedule(Integer.parseInt(hid),(current - 1) * size, size);
            lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(Schedule::getHid,hid);
            total = scheduleMapper.selectList(lqw2).size();
        }

        if (movieame != null && movieame.isBlank() == false && hid != null && hid.isBlank() == false){
            lqw.like(Movie::getName,movieame);
            movie = movieMapper.selectOne(lqw);
            allSchedule = scheduleMapper.findByMidAndHidSchedule(movie.getId(), Integer.parseInt(hid),(current - 1) * size, size);
            lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(Schedule::getHid,hid);
            lqw2.eq(Schedule::getMid,movie.getId());
            total = scheduleMapper.selectList(lqw2).size();
        }

        if ((movieame == null || movieame.isBlank()) && (hid == null || hid.isBlank() || hid.equals("0"))){
            allSchedule = scheduleMapper.findAllSchedule((current - 1) * size, size);
            total = scheduleMapper.selectList(null).size();
        }
        Schedule lastSchedule = new Schedule();
        lastSchedule.setTotal(total);
        allSchedule.add(lastSchedule);
        return allSchedule;
    }

    @RequestMapping("delSchedule")
    @Transactional
    public Result delSchedule(int id){
        int i = scheduleMapper.deleteById(id);
        if (i <= 0){
            return new Result(0,"删除失败!",null);
        }
        return new Result(1,"删除成功!",null);
    }

    @RequestMapping("modSchedule")
    @Transactional
    public Result modSchedule(@RequestBody Schedule schedule, Errors errors){
        if (errors.hasErrors()) {
            return new Result(0,"请完善排片信息",errors.getAllErrors());
        }

        LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Schedule::getId,schedule.getId());

        int update = scheduleMapper.update(schedule, lqw);
        if (update <= 0) {
            return new Result(0,"编辑排片失败",null);
        }

        return new Result(1,"编辑排片成功！",null);

    }

    @RequestMapping("findByIdSchedule")
    public Schedule findByIdSchedule(@RequestParam int id){
        Schedule schedule = scheduleMapper.findByIdSchedule(id);
        return schedule;
    }

}

